<?php

namespace app\models;

use Yii;
use yii\db\Exception;
/**
 * This is the model class for table "nea_zoushi".
 *
 * @property integer $id
 * @property string $data_date
 * @property string $data_time
 * @property double $data_open
 * @property double $data_close
 * @property double $data_highest
 * @property double $data_lowest
 * @property double $data_amount
 * @property double $data_base
 * @property integer $is_fenhong_point
 */
class NeaZoushi extends \yii\db\ActiveRecord
{
  /**
   * @var NEA涨跌的最小值
   */
  private $neaminvalue = -0.02;
  /**
   * @var NEA涨跌的最大值
   */
  private $neamaxvalue = 0.06;
  /**
   * @var K线图构造收盘开盘价最大振幅幅度比例
   */
  private $neaopenclosefactor = 0.08;
  /**
   * @var K线图构造最高价最低价最大振幅幅度比例
   */
  private $neahighlowfactor = 0.05;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'nea_zoushi';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['data_date', /*'data_time',*/ 'data_open', 'data_close', 'data_highest', 'data_lowest', 'data_amount', 'data_base', 'is_fenhong_point'], 'required'],
            [['data_open', 'data_close', 'data_highest', 'data_lowest', 'data_amount', 'data_base'], 'number'],
            [['is_fenhong_point'], 'integer'],
            [['data_date'], 'string', 'max' => 10],
            [['data_time'], 'string', 'max' => 8],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'data_date' => '日期',
            'data_time' => '时间',
            'data_open' => '开盘',
            'data_close' => '收盘',
            'data_highest' => '最高',
            'data_lowest' => '最低',
            'data_amount' => '成交额',
            'data_base' => '市盈率（分红）',
            'is_fenhong_point' => '是否账户划拨',
        ];
    }
    /*
    * 查询走势图数据
    */
    public function getNeazoushi()
    {
      $dataset="[";
      $nea_datas=NeaZoushi::find()->orderBy(['data_date'=>SORT_DESC])->limit(500)->asArray()->all();
      //按日期排序
      foreach($nea_datas as $key =>$value){
        $data_date_arr[$key]=$value['data_date'];
      }
      if(!empty($nea_datas)){
        array_multisort($data_date_arr,SORT_ASC,$nea_datas);
      }
      //构造字符串
      foreach($nea_datas as $nea_data){
        $array=[];
        //$array[0]=$nea_data['data_date'].' '.$nea_data['data_time'];
        $array[0]=$nea_data['data_date'];
        $array[1]=number_format($nea_data['data_base'],4);
        $array[2]=$nea_data['data_open'];
        $array[3]=$nea_data['data_close'];
        $array[4]=$nea_data['data_highest'];
        $array[5]=$nea_data['data_lowest'];
        $array[6]=$nea_data['data_amount'];
        $array[7]=$nea_data['is_fenhong_point'];
        $array[8]=$nea_data['id'];
        $dataset.=json_encode($array,JSON_NUMERIC_CHECK).",";
      }
      $dataset.="]";
      return $dataset;
    }

    public static function updateKlineitem($model){
      if(!$model->save()){
        print_r($model->errors);
        exit;
        return false;
      }
      return true;
    }
    public static function addKlineitem($model){
      $model->data_time='09:00:00';
      if(!$model->save()){
        return false;
      }
      return true;
    }

    /*
    * 插入数据
    */
    public function insertNeazoushi($data_base,$data_date,$data_time,$is_fenhong_point)
    {
        $data = new NeaZoushi;
        $data->data_base = $data_base;
        $data->data_date = $data_date;
        $data->data_time = $data_time;
        //计算K线图周边值
        //开盘价收盘价计算方法：以基本值为中值，随机数为振幅生成开盘价和收盘价(也即生成的开盘价和收盘价围绕中值对称)，随机数范围可配置（-0.02->0.06）*X%  x可配置
        //最高价最低价计算方法：以最大值或最小值为基值，随机数为振幅生成最高价和最低价，+-（-0.02->0.06）*Y%  y可配置。
        //最高价算出后如果小于开盘价或收盘价，取开盘价或收盘价，如果大于开盘价或收盘价，此值即为最高价，最低价算出后如果大于开盘价或收盘价，取开盘价或收盘价为最低价，否则取此值为最低价
        //open>close是跌，反之是升，查找前一条记录的base，如果本条的base>prev.base,设置open<close

        $a = $this->neamaxvalue - $this->neaminvalue;
        $max=getrandmax();
        $oca = rand()*$a/$max*$this->neaopenclosefactor/2.0;

        $ha=(rand()*$a/$max-$a/2)*$this->neahighlowfactor;
        $la=(rand()*$a/$max-$a/2)*$this->neahighlowfactor;
        $old_data = NeaZoushi::find()
                    ->orderBy(['data_date' => SORT_DESC, 'data_time' => SORT_DESC])
                    ->limit(1)
                    ->one();

        $open = 0.0;
        $close=0.0;
        $highest=0.0;
        $lowest=0.0;
        if(!isset($old_data)){
          //echo $ha;
          //exit;
          //跌
          $open=$data_base+$oca;
          $close=$data_base-$oca;

          if($ha<0){
            $highest=$open;
          }else{
            $highest=$open+$ha;
          }

          if($la>0){
            $lowest=$close;
          }else{
            $lowest=$close+$ha;
          }

        }else{

          if($old_data->data_base>$data_base){
            //echo $old_data->data_base.'='.$data_base;
            //exit;
            //跌
            $open=$data_base+$oca;
            $close=$data_base-$oca;

            if($ha<0){
              $highest=$open;
            }else{
              $highest=$open+$ha;
            }

            if($la>0){
              $lowest=$close;
            }else{
              $lowest=$close+$ha;
            }
          }else{
            //升
            $open=$data_base-$oca;
            $close=$data_base+$oca;

            if($ha<0){
              $highest=$close;
            }else{
              $highest=$close+$ha;
            }

            if($la>0){
              $lowest=$open;
            }else{
              $lowest=$open+$ha;
            }
          }
        }

        $data->data_open=$open;
        $data->data_close=$close;
        $data->data_highest=$highest;
        $data->data_lowest=$lowest;
        $data->is_fenhong_point=$is_fenhong_point;
        //var_dump($data);
      //  exit;
        return $data->insert();


    }

}
